package com.woorigil.android.sample.activity.product.detail;

import java.util.List;

import com.woorigil.android.sample.activity.R;
import com.woorigil.android.sample.domain.product.Deal;
import com.woorigil.android.sample.support.DisplayUtils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;

public class ProductDetailPageAdapter extends FragmentStatePagerAdapter {
	private Context mContext;
	private int mResource;
	private List<Deal> deals;
	private LayoutInflater mInflater;
	
	public ProductDetailPageAdapter(Context context, int layoutResource, FragmentManager fm, List<Deal> deals) {
        super(fm);
        this.mContext = context;
		this.mResource = layoutResource;
		this.deals = deals;
		this.mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

	@Override
	public Fragment getItem(int position) {
		return DetailFragment.newInstance(mContext, mInflater, mResource, deals.get(position));
    }
	
	@Override
	public int getItemPosition(Object item) {
		DetailFragment fragment = (DetailFragment)item;
        int position = deals.indexOf(fragment.getDeal());

        if (position >= 0) {
            return position;
        } else {
            return POSITION_NONE;
        }
    }
	
	@Override
	public int getCount() {
		return deals.size();
	}

	public void clear() {
		deals.clear();
	}
	
	public void add(Deal deal) {
		deals.add(deal);
	}
	
	public static class DetailFragment extends Fragment {
		private static final String KEY_CONTENT = "TestFragment:Content";
		private Deal deal;
		private Context mContext;
		private int mResource;
		private LayoutInflater mInflater;
		
		public static DetailFragment newInstance(Deal deal) {
			DetailFragment fragment = new DetailFragment();
			fragment.deal = deal;
			return fragment;
	    }
		
		public static Fragment newInstance(Context mContext, LayoutInflater mInflater, int mResource, Deal deal) {
			DetailFragment fragment = new DetailFragment();
			fragment.deal = deal;
			fragment.mContext = mContext;
			fragment.mResource = mResource;
			fragment.mInflater = mInflater;
			return fragment;
		}
		
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
			if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
//					title = savedInstanceState.getString(KEY_CONTENT);
			}
			
			View convertView = mInflater.inflate(mResource, container, false);
			
			WebView image = (WebView)convertView.findViewById(R.id.deal_image);
			image.setFocusable(false);
			LayoutParams layoutParams = image.getLayoutParams();
			layoutParams.width = DisplayUtils.widthPixels(mContext);
			layoutParams.height = (int)(layoutParams.width * 0.5);
			image.setLayoutParams(layoutParams);
			TextView title = (TextView) convertView.findViewById(R.id.detail_title);
			TextView originalPrice = (TextView) convertView.findViewById(R.id.detail_original_price);
			TextView salesPrice = (TextView) convertView.findViewById(R.id.detail_sales_price);
			TextView discountRate = (TextView) convertView.findViewById(R.id.detail_discount_rate);
			TextView nowCount = (TextView) convertView.findViewById(R.id.detail_now_count);
			TextView saleEndAt = (TextView) convertView.findViewById(R.id.detail_sale_end_date);
			TextView providerName = (TextView) convertView.findViewById(R.id.detail_provider_name);
						
			if (image != null) {
				StringBuffer sb = new StringBuffer();
				sb.append("<html><body style='padding:0;margin:0;'>");
				sb.append("<img src='" + deal.getThumbnailImageUrl1() + "' style='width:100%;height:100%;padding:0;margin:0' />");
				sb.append("</body></html>");
				image.loadDataWithBaseURL(null, sb.toString(), "text/html", "utf-8", null);
			}
			title.setText(deal.display1());
			originalPrice.setText(DisplayUtils.numFormat(deal.getOriginalPrice()) + "원");
			originalPrice.setPaintFlags(originalPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
			salesPrice.setText(DisplayUtils.numFormat(deal.getSalesPrice()) + "원");
			discountRate.setText(deal.getDiscountRate() + "%");
			nowCount.setText("구매중 " + DisplayUtils.numFormat(deal.getNowCount()) + "개");
			saleEndAt.setText(DisplayUtils.dateFormat(Long.parseLong(deal.getToDate())) + "까지");
			providerName.setText("[" + deal.getProviderCode().getDisplay() + "]");
			
	        Button detailButton = (Button)convertView.findViewById(R.id.detailButton);
	        detailButton.setOnClickListener(new OnClickListener() {
	        	public void onClick(View v) {
	        		Uri uri = Uri.parse(deal.getDetailViewUrl());
	    			startActivity(new Intent(Intent.ACTION_VIEW, uri));
	        	}
	        });
	        
//	        LinearLayout layout = (LinearLayout)convertView.findViewById(R.id.detailDotPanel);
//	        createDotPanel(mContext, layout, 3);
//	        selectDealDot(position);
			return convertView;
		}

		@Override
		public void onSaveInstanceState(Bundle outState) {
			super.onSaveInstanceState(outState);
			outState.putString(KEY_CONTENT, deal.getTitle());
		}

		public Deal getDeal() {
			return deal;
		}
	}
}
